//该程序必须在特权命令下运行，否则ctrl+z的信号无法起作用
#include <stdio.h>
#include <stdlib.h>
#include <sched.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <wait.h>
#include <unistd.h>
#include <signal.h>
#define WAIT_SE 1	//间隔秒数
#define EPOCH 20	//试验轮数
typedef void (*sighandler_t) (int);
int pid=0;
void add(){
	if(pid!=0)	//如果是父进程
		setpriority(PRIO_PROCESS,getpid(),(getpriority(PRIO_PROCESS,getpid())+1));//优先级加一
}
void minus(){
	if(pid==0)	//如果是子进程  
		setpriority(PRIO_PROCESS,getpid(),(getpriority(PRIO_PROCESS,getpid())-1));//优先级减一

}
int main()
{
	signal(SIGINT,(sighandler_t)add);	//注册信号处理事件
	signal(SIGTSTP,(sighandler_t)minus);	
	pid=fork();	//创建进程
	if(pid>0)	//如果是父进程
	{
		for(int i=0;i<EPOCH;i++){
			//打印十次，每次间隔2s
			sleep(WAIT_SE);
			printf("I'm father,my pid id %d,my priority is %d,my scheduler is %d\n",getpid(),getpriority(PRIO_PROCESS,getpid()),sched_getscheduler(getpid()));
			sleep(WAIT_SE);
		}
	}else if(pid==0){
		setpriority(PRIO_PROCESS,getpid(),5);//为观察现象设置子进程优先数为5
		for(int i=0;i<EPOCH;i++){
			sleep(WAIT_SE);
			printf("I'm son,my pid is %d,my priority is %d,my scheduler is %d\n",getpid(),getpriority(PRIO_PROCESS,getpid()),sched_getscheduler(getpid()));
			sleep(WAIT_SE);
		}
	}else{
		puts("fork error!");
		exit(EXIT_FAILURE);
	}
	return EXIT_SUCCESS;	
}
